package apartado2;

import aima.core.search.framework.HeuristicFunction;
import aima.core.util.datastructure.XYLocation;

public class FuncionHeuristicaManhattan implements HeuristicFunction{
	public double h(Object estado) {
		OchoPuzzleTablero tablero = (OchoPuzzleTablero) estado;
		int retVal = 0;
		for (int i = 1; i < 9; i++) {
			XYLocation pos = tablero.getPosicionOf(i);
			retVal += evaluarDistanciaManhattanDe(i, pos);
		}
		return retVal;

	}

	public int evaluarDistanciaManhattanDe(int i, XYLocation pos) {
		int retVal = -1;
		int xpos = pos.getXCoOrdinate();
		int ypos = pos.getYCoOrdinate();
		switch (i) {

		case 1:
			retVal = Math.abs(xpos - 0) + Math.abs(ypos - 1);
			break;
		case 2:
			retVal = Math.abs(xpos - 0) + Math.abs(ypos - 2);
			break;
		case 3:
			retVal = Math.abs(xpos - 1) + Math.abs(ypos - 0);
			break;
		case 4:
			retVal = Math.abs(xpos - 1) + Math.abs(ypos - 1);
			break;
		case 5:
			retVal = Math.abs(xpos - 1) + Math.abs(ypos - 2);
			break;
		case 6:
			retVal = Math.abs(xpos - 2) + Math.abs(ypos - 0);
			break;
		case 7:
			retVal = Math.abs(xpos - 2) + Math.abs(ypos - 1);
			break;
		case 8:
			retVal = Math.abs(xpos - 2) + Math.abs(ypos - 2);
			break;

		}
		return retVal;
	}
}
